<?php
namespace Api\Controller;

use Think\Controller;

class TableController extends Controller
{
    /**
     * 桌位列表
     */
    public function tableList()
    {
        $map['store_id'] = I("store_id");// 店铺ID
        $map['table_id'] = I("table_id");// 桌号

        $result = M('dishes_people')->where($map)->select();
        foreach ($result as $key => $v) {
            if ($v['create_time']) {
                $result[$key]['create_formart_time'] = date('Y-m-d', $v['create_time']);
            } else {
                $result[$key]['create_formart_time'] = '';
            }
            switch ($v['model']) {
                case 1:
                    $result[$key]['formart_model'] = '小桌';
                    break;
                case 2:
                    $result[$key]['formart_model'] = '中桌';
                    break;
                case 3:
                    $result[$key]['formart_model'] = '大桌';
                    break;
                default : $result[$key]['formart_model'] = '小桌';
            }
        }
        if ($result) {
            $this->ajaxReturn(array('state' => 1, 'data' => $result), 'JSON');
        } else {
            $this->ajaxReturn(array('state' => -1, 'data' => array()), 'JSON');
        }
    }

    /**
     * 本店所有商品分类（菜的分类）
     */
    public function foodClassify()
    {
        $data['store_id']   = I('post.store_id');   // 店铺ID
        $page   = I('post.page');   // 页数

        $result = M('dishes_category')->field('name,description')->where($data)->page($page, 10)->select();// 分类列表
        if ($result) {
            $this->ajaxReturn(array('state' => 1, 'data' => $result), 'JSON');
        } else {
            $this->ajaxReturn(array('state' => -1, 'data' => array()), 'JSON');
        }

    }

    /**
     * 本店所有商品（菜名）
     */
    public function food()
    {
        $page        = I('post.page','1');       // 分页数
        $data['store_id']   = I('post.store_id');   // 店铺ID

        $map['is_up'] = 1;
        $result = M('dishes')->field("id,name,description,price,image,num")->where($map)->where('num','GT',0)->page($page, 10)->select();
        if ($result) {
            $this->ajaxReturn(array('state' => 1, 'data' => $result), 'JSON');
        } else {
            $this->ajaxReturn(array('state' => -1, 'data' => array()), 'JSON');
        }

    }

    /**
     * 通过分类查询菜名
     */
    public function foodList()
    {
        $page        = I('post.page','1');       // 分页数
        $map['store_id']    = I('post.store_id');   // 店铺ID
        $map['category_id'] = I("post.category_id");// 分类ID

        $map['is_up'] = 1;
        $result = M('dishes')->field("id,name,description,price,image,num")->where($map)->where('num','GT',0)->page($page, 10)->select();
        if ($result) {
            $this->ajaxReturn(array('state' => 1, 'data' => $result), 'JSON');
        } else {
            $this->ajaxReturn(array('state' => -1, 'data' => array()), 'JSON');
        }

    }

    /**
     * 菜品属性的列表
     */
    public function attributesList()
    {
        $data['d.dish_id']  = I('post.food_id');  //菜的ID

        $result = M('dish_attributes')->alias("d")->join('meal_attributes c on c.id = d.attribute_id')->field('d.dish_id,d.attribute_value,d.is_need,c.name_attribute')->where($data)->select();
        foreach ($result as $key => $val) {
            $attr = array_filter(explode(',',$val['attribute_value']));
            $rs['key'] = $val['name_attribute'];
            foreach ($attr as $v) {
                $value = M("attributes_value")->field('id,value,price')->where("id=".$v)->find();
                $rs['value'][] = $value;
            }
        }
        $rs2[] = array("key"=>$rs['key'],'value'=>$rs['value']);
        if ($rs2) {
            $this->ajaxReturn(array('state' => 1, 'data' => $rs2), 'JSON');
        } else {
            $this->ajaxReturn(array('state' => -1, 'data' => array()), 'JSON');
        }
    }


}